﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using POSSystem.DAL;

namespace POSSystem.BLL
{
    public class CustomerType: _CustomerType
    {
        public static int DefaultType = 1;

        public static bool IsGroupEmpty(int id)
        {
            try
            {
                Customers c = new Customers();
                c.Where.Deleted.Value = false;
                c.Where.CustomerType.Value = id;
                c.Query.Top = 1;
                c.Query.Load();

                return c.RowCount <= 0;
            }
            catch (Exception e)
            {
                ErrorLog.WriteToFile("CustomerType.cs", "IsGroupEmpty", e.StackTrace);
                throw e;
            }
        }

        public static CustomerType GetAllActiveCustomerType()
        {
            try
            {
                CustomerType customerTypeTb = new CustomerType();
                customerTypeTb.Where.Deleted.Value = false;
                customerTypeTb.Where.BranchID.Value = Branches.CurrentBranchID;
                customerTypeTb.Query.Load();

                return customerTypeTb;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("CustomerType.cs", "GetAllActiveCustomerType", ex.StackTrace);
                throw new Exception("CustomerType.GetAllActiveCustomerType(): " + ex.Message);
            }
        }

        public static CustomerType GetAllActiveCustomerType(int branchID)
        {
            try
            {
                CustomerType customerTypeTb = new CustomerType();
                customerTypeTb.Where.Deleted.Value = false;
                customerTypeTb.Where.BranchID.Value = branchID;
                customerTypeTb.Query.Load();

                return customerTypeTb;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("CustomerType.cs", "GetAllActiveCustomerType", ex.StackTrace);
                throw new Exception("CustomerType.GetAllActiveCustomerType(): " + ex.Message);
            }
        }

        public static CustomerType AddNewCustomerType(string customerTypeName, bool isDealerPrice)
        {
            try
            {
                CustomerType customerTypeTb = new CustomerType();
                customerTypeTb.AddNew();
                //fields

                customerTypeTb.TypeName = customerTypeName;
                customerTypeTb.DealerPrice = isDealerPrice;
                customerTypeTb.Deleted = false;
                customerTypeTb.LastModifiedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                //
                customerTypeTb.Save();
                return customerTypeTb;
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("CustomerType.cs", "AddNewCustomerType", ex.StackTrace);
                throw new Exception("CustomerType.AddNewCustomerType(): " + ex.Message);
            }
        }

        public class CustomerPrice
        {
            public const int SalePrice = 1;
            public const int DealerPrice = 2;
            public const int PartnerPrice = 3;
        }
    }
}
